package com.example.common.tools

import java.math.BigDecimal

class NumUtil {

    companion object {
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            NumUtil()
        }
    }

    /**
     *除法  保留2位小数
     */
    fun divide2(num1: Float, num2: Float): Float {
        val b1 = BigDecimal.valueOf(num1.toDouble())
        val b2 = BigDecimal.valueOf(num2.toDouble())
        val decimal = b1.divide(b2, 3)
        LogUtils.eLog("NumUtil", "num1  = $num1  num2 = $num2  b1 = $b1  b2 = $b2")
        return decimal.setScale(2, BigDecimal.ROUND_HALF_UP).toFloat()
    }

    /**
     * 小数转百分比
     * 翻转百分比保留整数
     */
    fun evertPer(num: Float): Int {
        val b1 = BigDecimal.valueOf((1 - num).toDouble())
        val b2 = BigDecimal.valueOf(100)
        val decimal = b1.multiply(b2)
        return decimal.setScale(0,BigDecimal.ROUND_HALF_UP).toInt()
    }

    /**
     * 百分比转小数
     * 翻转百分比保留整数
     */
    fun evertFloat2(num: Int): Float {
        val b1 = BigDecimal.valueOf((100 - num).toDouble())
        val b2 = BigDecimal.valueOf(100)
        val decimal = b1.divide(b2)
        return decimal.toFloat()
    }
}

